import Vue from 'vue'
import VueRouter from 'vue-router'
import {get} from "../api/index"
// import r from "../../public/route"
Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect: "/home"
  },
  {
    path:"/register",
    name:"Register",
    meta:{
      title:"用户注册"
    },
    component:()=>import("../views/Register")
  },
  {
    path:"/login",
    name:"Login",
    meta:{
      title:"用户登录"
    },
    component:()=>import("../views/Login")
  },
  {
    path:"/home",
    name:"Home",
    meta:{
      title:"主页",
      isverify:"true"
    },
    component:()=>import("../views/Home")
  },
  {
    path:"/detail/:aid",
    name:"Detail",
    meta:{
      title:"详情页",
      isverify:"true"
    },
    props:true,

    component:()=>import("../views/Detail")
  },
  {
    path:"/post",
    name:"Post",
    meta:{
      title:"发布新话题",
      isverify:"true"
    },
    component:()=>import("../views/Post")
  },
  {
    path:"/personal",
    name:"Personal",
    meta:{
      title:"个人中心",
      isverify:"true"
    },
    component:()=>import("../views/Personal")
  },
  {
    path:"/perinfo",
    name:"PerInfo",
    meta:{
      title:"基础设置",
      isverify:"true"
    },
    component:()=> import("../views/PerInfo")
  },
  {
    path:"/category",
    name:"Category",
    meta:{
      title:"文章类别",
      isverify:"true"
    },
    component:()=> import("../views/Category")
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if(to.meta.isverify){
 
      get("/isLogin.php").then((res)=>{
        // console.log(res);
        if(res.ret === 200){
          next()
        }else{
          next("/login")
        }
      })   
  }else{
    next()
  } 
})
router.afterEach( to => {
  document.title = to.meta.title
})

export default router
